package one.bugu.android.demon.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.juefeng.android.framework.common.base.BaseListAdapter;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.BGTInfoBean;
import one.bugu.android.demon.bean.MessageBean;
import one.bugu.android.demon.ui.fragment.snatch.MessageFragment;
import one.bugu.android.demon.util.MyTextUtils;

/**
 * 站内信息列表适配
 */
public class MessageAdapter extends BaseListAdapter<MessageBean> {

    private Context context;

    private int pageType;

    public MessageAdapter(Context context, int pageType) {
        super(context);
        this.context = context;
        this.pageType = pageType;
    }

    @Override
    public int getContentLayoutId() {
        return R.layout.item_message;
    }

    @Override
    public ViewHolder getViewHolder() {
        return new Holder();
    }

    public class Holder implements ViewHolder<MessageBean> {

        /**
         * 时间
         */
        private TextView time;

        /**
         * 标题
         */
        private TextView title;

        /**
         * 类型
         */
        private TextView type;

        /**
         * 内容
         */
        private TextView content;
        private LinearLayout llMsgType;
        private LinearLayout llMsgContent;
        private TextView tvSysMsg;

        @Override
        public void initView(View view) {
            time = view.findViewById(R.id.message_time);
            title = view.findViewById(R.id.message_title);
            type = view.findViewById(R.id.message_type);
            content = view.findViewById(R.id.message_content);
            llMsgType = view.findViewById(R.id.ll_warn_msg);
            llMsgContent = view.findViewById(R.id.ll_warn_msg_content);
            tvSysMsg = view.findViewById(R.id.tv_sys_msg);
        }

        /**
         * 设置预警信息文字
         *
         * @param bgtInfoBean
         */
        @SuppressLint("SetTextI18n")
        @Override
        public void initData(MessageBean bgtInfoBean) {
            llMsgType.setVisibility(pageType == MessageFragment.WARN_MSG ? View.VISIBLE : View.GONE);
            llMsgContent.setVisibility(pageType == MessageFragment.WARN_MSG ? View.VISIBLE : View.GONE);
            tvSysMsg.setVisibility(pageType == MessageFragment.WARN_MSG ? View.GONE : View.VISIBLE);
            time.setText(bgtInfoBean.getCreateTime() != null ? bgtInfoBean.getCreateTime().replace(".0", "") : "");
            if (pageType == MessageFragment.WARN_MSG) {
                //预警
                title.setText(bgtInfoBean.getPlatformName() + "-" + bgtInfoBean.getCoinName() + "最新价格");
                content.setText(bgtInfoBean.getContent());
                type.setText("价格预警");
            } else {
                String content = bgtInfoBean.getContent();
                if (TextUtils.isEmpty(content)) {
                    content = "";
                }
                if (bgtInfoBean.getRemindType() == 90) {
                    //中奖消息
                    title.setText("中奖提醒");
                    int startIndex = content.indexOf("“");
                    int endIndex = content.indexOf("”");
                    if (startIndex != -1 && endIndex != -1) {
                        SpannableString spannableString = MyTextUtils.getInstance().setAutoTextColor(content, startIndex + 1, endIndex, "#5856D0");
                        tvSysMsg.setText(spannableString);
                    } else {
                        tvSysMsg.setText(content);
                    }
                } else if (bgtInfoBean.getRemindType() == 100) {
                    //提现
                    title.setText("提现结果");
                    String pattern = "[0-9.]{1,}ETH|(0x)?[0-9a-fA-F]{40}";
                    SpannableString spannableString = MyTextUtils.getInstance().setAutoTextColor(content, pattern, "#5856D0");
                    tvSysMsg.setText(spannableString);
                } else {
                    title.setText("系统消息");
                    tvSysMsg.setText(content);
                }

            }
        }

        @Override
        public void initEvent(MessageBean bgtInfoBean) {

        }
    }
}
